package ru.urfc.portal.ui.client.googlets.client.documents;

import ru.urfc.portal.ui.client.BaseDocument;
import ru.urfc.portal.ui.client.Utils;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;
import ru.urfc.portal.ui.client.widgets.SelectDevices;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Widget;

public class ServiceOrder extends BaseDocument {

	interface Binder extends UiBinder<Widget, ServiceOrder> {}
	private static Binder binder = GWT.create(Binder.class);
	
	@UiField CheckBox callsignAssignmentCheck;
	@UiField Element callsignAssignmentForm;
	@UiField TextField<String> callsign;
	@UiField TextField<String> callsignNumber;
	@UiField CheckBox measurementsCheck;
	@UiField Element measurementsForm;
	@UiField TextField<String> frequenciesRange;
	@UiField TextField<String> objective;
	@UiField TextField<String> measuredParameters;
	@UiField DateField deadline;
	@UiField CheckBox documentExpertiseCheck;
	@UiField SelectDevices selectDevices;
	@UiField Element selectDevicesForm;
	
	@Override
	protected void init(GridDescriptor descriptor) {
		initWidget(binder.createAndBindUi(this));
		initCheckBox(callsignAssignmentCheck, callsignAssignmentForm);
		initCheckBox(measurementsCheck, measurementsForm);
		initCheckBox(documentExpertiseCheck, selectDevicesForm);

		descriptor.setAddFieldPathPrefix("data");
		descriptor.addBoolField("callsignAssignmentCheck", callsignAssignmentCheck);
		descriptor.addTextField("callsign", callsign);
		descriptor.addTextField("callsignNumber", callsignNumber);
		descriptor.addBoolField("measurementsCheck", measurementsCheck);
		descriptor.addTextField("frequenciesRange", frequenciesRange);
		descriptor.addTextField("objective", objective);
		descriptor.addTextField("measuredParameters", measuredParameters);
		descriptor.addDateField("deadline", deadline);
		descriptor.addBoolField("documentExpertiseCheck", documentExpertiseCheck);
		descriptor.setAddFieldPathPrefix("");
		
		bind(selectDevices);
	}

	private void initCheckBox(final CheckBox checkBox, final Element element) {
		checkBox.addListener(Events.Change, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent event) {
				if (checkBox.getValue()) {
					Utils.show(element);
				} else {
					Utils.hide(element);
				}
			}
		});
	}

}
